//
//  LoginViewController.m
//  text
//
//  Created by 郑州动力无限科技 on 2017/3/7.
//  Copyright © 2017年 郑州动力无限科技. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPwdViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
{
    FXTUserModel * _userModel;
}
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
- (IBAction)loginIn:(id)sender;
- (IBAction)forgetPassword:(id)sender;

@end

@implementation LoginViewController

#pragma mark - 子类继承
-(CMNavType)getNavType {
    return CMNavTypeNoLeftItem;
}
- (void)viewDidLoad {
    self.title = @"登录";
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initData];
    [self initNav];
}
-(void)initNav{
    UIButton *  tempBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [tempBtn setAdjustsImageWhenHighlighted:NO];
    tempBtn.frame = CGRectMake(0, 0, 80, 44);
    tempBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:16.0f];
    [tempBtn setTitle:@"注册" forState:UIControlStateNormal];
    [tempBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [tempBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tempBtn addTarget:self action:@selector(registerUsers) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem=[[UIBarButtonItem alloc] initWithCustomView:tempBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}
-(void)initData{
    _userModel = [FXTUserTools UserAccount];
    if (_userModel.token==nil) {
    _userModel = [[FXTUserModel alloc] init];
    }
    [_forgetButton setAdjustsImageWhenDisabled:NO];
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.layer.cornerRadius = 5.0f;
    _phoneTF.delegate = self;
    [_phoneTF addTarget:self action:@selector(limitTextlength:) forControlEvents:UIControlEventEditingChanged];
    _passwordTF.delegate = self;
    [_passwordTF addTarget:self action:@selector(limitTextlength:) forControlEvents:UIControlEventEditingChanged];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma -  TouchFounction
- (IBAction)loginIn:(id)sender{
    
    if ([FXTPublicTools isBlankString:_phoneTF.text]==YES) {
        [DisplayHelper displayWarningAlert:@"请输入手机号"];
    }else if ([FXTPublicTools isBlankString:_passwordTF.text]==YES){
        [DisplayHelper displayWarningAlert:@"请输入密码"];
    }else{
        NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
        [paramDic setValue:@1 forKey:@"login_type"];
        [paramDic setValue:_phoneTF.text forKey:@"phe"];
        [paramDic setValue:_passwordTF.text forKey:@"pwd"];

        [self loginRequest:paramDic];
    }
}
- (IBAction)forgetPassword:(id)sender{
    ForgetPwdViewController * forgetVC = [[ForgetPwdViewController alloc] init];
    forgetVC.title = @"找回密码";
    forgetVC.isForget = YES;
    [self.navigationController pushViewController:forgetVC animated:YES];
}
- (void)registerUsers{
    RegisterViewController * registerVC = [[RegisterViewController alloc] init];
    registerVC.title = @"注册";
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark   -   Request
-(void)loginRequest:(NSMutableDictionary*)paramDic{
    
    FXTHttpRequestModel * requestModel = [[FXTHttpRequestModel alloc] init];
    requestModel.type = FXTHttpType_POST;
    requestModel.appendUrl = kLogin_AppLogin;
    [requestModel.paramDic setObject:self.phoneTF.text forKey:@"telephone"];
    [requestModel.paramDic setObject:self.passwordTF.text forKey:@"password"];
    WS(ws);
    [[DisplayHelper shareDisplayHelper]showLoading:self.view noteText:@"登录中..."];
    requestModel.callback = ^(FXTHttpResponseModel * result, NSError *error){
        if (result.state ==FXTReponseCodeState_Success) {
            
            [DisplayHelper displaySuccessAlert:result.alertMsg];
            NSDictionary * dic = result.data ;
            FXTUserModel * userMdl = [FXTUserModel dicWithUserAccount:dic];
            [FXTUserTools saveUserAccount:userMdl];
            [[FXTPublicTools getAppDelegate] initRootViewController];
            
        }else{
            [DisplayHelper displayWarningAlert:result.alertMsg];
        }
        [[DisplayHelper shareDisplayHelper]hideLoading:ws.view];
    };
    [[FXTHttpSessionManager sharedHttpSessionManager] sendHttpRequestParam:requestModel];
}
#pragma mark   -     UITextFildDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_phoneTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
    return YES;
}

#pragma mark 输入长度限制
- (void)limitTextlength:(UITextField *)textField
{
    if ([textField.text length]>12)
    {
        textField.text=[textField.text substringToIndex:11];
    }
}



@end
